let ImportService = require('./ImportService')
let Mongodb = require('../../database/mongodb');
var bson = require('bson');

class TestCaseService extends ImportService{
  initParam () {
    this.enumTypes = ['BACKLOG_LEVEL', 'MODULE_STATE']
  }

  formateData (dataList, { productId }) {
    return new Promise((resolve, reject) => {
      let rowDatas = []
      dataList.forEach(data => {
        let item = {
          "_id": bson.ObjectId(),
          "name": String(data[0]).trim(),
          "pname": String(data[1]).trim(),
          "level": data[2],
          "productId": Number(productId),
          "important": this.dataFormater('BACKLOG_LEVEL', data[3]),
          "manage": String(data[4]).trim() === '' ? false : true,
          "app": String(data[5]).trim() === '' ? false : true,
          "pc": String(data[6]).trim() === '' ? false : true,
          "web": String(data[7]).trim() === '' ? false : true,
          "pad": String(data[8]).trim() === '' ? false : true,
          "po": this.dataArrFormatter('users', data[9], 'email', 'userName'),
          "ui": this.dataArrFormatter('users', data[10], 'email', 'userName'),
          "test": this.dataArrFormatter('users', data[11], 'email', 'userName'),
          "om": this.dataArrFormatter('users', data[12], 'email', 'userName'),
          "dev": this.dataArrFormatter('users', data[13], 'email', 'userName'),
          "orgId": this.user.orgId[0],
          "createDate": new Date(),
          "createUser": this.user.email,
          "states": this.processState(data[14]),
          "sort": data[15]
        }
        rowDatas.push(item)
      })
      resolve(rowDatas)
    })
  }

  processState (states) {
    let result = []
    if (states) {
      let stateList = states.split('\n\t')
      let state = []
      stateList.forEach(item => {
        state = item.split('/')
        result.push({
          "year" : new Date(state[0], 1, 1),
          "state" : this.dataFormater('MODULE_STATE', state[1], 'value', 'name'),
          "id" : String(bson.ObjectId()),
          "createUser" : this.user.email,
          "createDate" : new Date()
        })
      })
    }
    return result
  }

  saveData (tableName, docs, { productId }) {
    return new Promise((resolve, reject) => {
      if (docs.length > 0) {
        let condition = {
          productId: productId
        }
        Mongodb.find(tableName, condition, ['_id', 'name'], {}, (err, hasDataList) => {
          let idMap = new Map()
          for (let index = 0; index < hasDataList.length; index++) {
            const element = hasDataList[index];
            idMap.set(element.name, element)
          }

          this.processPid(docs, idMap)
          for (let index = 0; index < docs.length; index++) {
            const element = docs[index];
            if (element.name && idMap.has(element.name)) {
              element._id = idMap.get(element.name)._id
              Mongodb.update(tableName, { _id: idMap.get(element.name)._id }, element, (err, docs) => {});
            } else {
              Mongodb.save(tableName, element, (err, docs) => {});
            }
          }
        })
      }
    });
  }

  processPid (docs, hasDataMap) {
    let newDataMap = new Map()
    docs.forEach((doc) => {
      newDataMap.set(doc.name, doc)
    })
    console.log(docs)
    console.log(newDataMap)
    docs.forEach((doc) => {
      if (doc.pname) {
        console.log(doc.pname)
        if (hasDataMap.has(doc.pname)) {
          doc.pid = String(hasDataMap.get(doc.pname)._id)
        } else if (newDataMap.has(doc.pname)) {
          doc.pid = String(newDataMap.get(doc.pname)._id)
        }
        delete doc.pname
      }
    })
  }

}

module.exports = new TestCaseService()